/**
 * 创建新数组根据指定深度打平数组
 */
const arr = [1, [2, [3, [4, [5]]]]];
console.log(arr.flat(2));

function myFlat(index = 1) {
  let result = [];
  for (let i = 0; i < this.length; i++) {
    result.push(this[i]);
  }
  for (let i = 0; i < index; i++) {
    let length = result.length;
    for (let j = 0; j < length; j++) {
      if (Array.isArray(result[j])) {
        result.splice(j, 1, ...result[j]);
      }
    }
  }
  return result;
}

Array.prototype.myFlat = myFlat;
console.log(arr.myFlat(3));
